package com.jobmanagementsystem.controller;

import com.jobmanagementsystem.common.CommonResult;
import com.jobmanagementsystem.entity.User;
import com.jobmanagementsystem.entity.UserRoleMenuRelation;
import com.jobmanagementsystem.entity.UserRolePermissionRelation;
import com.jobmanagementsystem.entity.UserRoleResourceRelation;
import com.jobmanagementsystem.service.IUserRoleMenuRelationService;
import com.jobmanagementsystem.service.IUserRolePermissionRelationService;
import com.jobmanagementsystem.service.IUserRoleResourceRelationService;
import com.jobmanagementsystem.service.UserService;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "/user",name = "用户")
public class UserController {

  private final UserService userService;
  private final IUserRolePermissionRelationService userRolePermissionRelationService;
  private final IUserRoleMenuRelationService userRoleMenuRelationService;
  private final IUserRoleResourceRelationService userRoleResourceRelationService;

  public UserController(UserService userService,
      IUserRolePermissionRelationService userRolePermissionRelationService,
      IUserRoleMenuRelationService userRoleMenuRelationService,
      IUserRoleResourceRelationService userRoleResourceRelationService) {
    this.userService = userService;
    this.userRolePermissionRelationService = userRolePermissionRelationService;
    this.userRoleMenuRelationService = userRoleMenuRelationService;
    this.userRoleResourceRelationService = userRoleResourceRelationService;
  }

  @GetMapping(value = "/index/login")
  public ModelAndView index() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("system/login");
    return modelAndView;
  }

  @GetMapping(value = "/userManage")
  public ModelAndView userManage() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("system/usermanage/usermanage");
    return modelAndView;
  }

  @PostMapping(value = "/login",name = "登录")
  public CommonResult<User> login(User user) {
    User loginUser = userService.getUserByAccount(user.getUserAccount());
    if (loginUser == null || !loginUser.getUserPwd().equals(user.getUserPwd())) {
      return CommonResult.generateFailureResult("帐号或密码不正确", 1, null);
    } else {
      String url = String.format("http://%s:%s/employmentManagement/index", "localhost", "8080");
      loginUser.setUrl(url);
      return CommonResult.generateSuccessResult(1, loginUser);
    }
  }

  @PostMapping(value = "/getAllUsers",name = "列表")
  public CommonResult<List<User>> getAllUsers(User user, @RequestParam("limit") int pageSize,
      @RequestParam("page") int pageNum) {
    List<User> result = userService.getAllUsers(user, pageNum, pageSize);
    return CommonResult.generateSuccessResult(result.size(), result);
  }

  @GetMapping(value = "/getUserByAccount/{userAccount}",name = "用户数量")
  public CommonResult<User> getUserByAccount(@PathVariable("userAccount") String userAccount) {
    return CommonResult.generateSuccessResult(1, userService.getUserByAccount(userAccount));
  }

  @PostMapping(value = "/addUser",name = "添加用户")
  public CommonResult<Integer> addUser(User user) {
    user.setUserId(UUID.randomUUID().toString());
    userService.addUser(user);
    UserRolePermissionRelation userRolePermissionRelation = new UserRolePermissionRelation();
    UserRoleMenuRelation userRoleMenuRelation = new UserRoleMenuRelation();
    UserRoleResourceRelation userRoleResourceRelation = new UserRoleResourceRelation();
    if ("0".equals(user.getUserType())){
      userRolePermissionRelation.setPermissionId(1L);
      userRolePermissionRelation.setRoleId(1L);
      userRoleMenuRelation.setRoleId(1L);
      userRoleMenuRelation.setMenuId(1L);
      userRoleResourceRelation.setRoleId(1L);
      userRoleResourceRelation.setResourceId(1L);
    }else{
      userRolePermissionRelation.setPermissionId(2L);
      userRolePermissionRelation.setRoleId(2L);
      userRoleMenuRelation.setRoleId(2L);
      userRoleMenuRelation.setMenuId(2L);
      userRoleResourceRelation.setRoleId(2L);
      userRoleResourceRelation.setResourceId(2L);
    }
    userRolePermissionRelation.setUserId(user.getUserId());
    userRolePermissionRelationService.getBaseMapper().insert(userRolePermissionRelation);
    userRoleMenuRelationService.getBaseMapper().insert(userRoleMenuRelation);
    userRoleResourceRelationService.getBaseMapper().insert(userRoleResourceRelation);
    return CommonResult.generateSuccessResult(1, 1);
  }

  @PutMapping(value = "/updateUser",name = "修改用户")
  public CommonResult<Integer> updateUser(User user) {
    userService.updateUser(user);
    return CommonResult.generateSuccessResult(1, 1);
  }

  @PostMapping(value = "/delInfo/{userId}",name = "删除")
  public CommonResult<Integer> delInfo(@PathVariable("userId") String userId) {
    userService.deleteUser(userId);
    return CommonResult.generateSuccessResult(1, 1);
  }
}
